package com.pap.rbac.roleresource.agent.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.rbac.agent.roleresource.IRoleResourceRelAgent;
import com.pap.rbac.roleresource.auto.entity.RoleResourceRel;
import com.pap.rbac.roleresource.auto.mapper.RoleResourceRelMapper;

@Transactional
@Service("roleResourceRelAgent")
public class RoleResourceRelAgentImpl extends PapBaseServiceImpl<RoleResourceRel> implements IRoleResourceRelAgent {

	@Autowired
	private RoleResourceRelMapper roleResourceRelMapper;

	@Override
	public void updateRoleResourceRel(String clientLicenseId, String roleId, List<String> resourceLists) {
		roleResourceRelMapper.deleteByRoleId(roleId);
		if(resourceLists != null) {
			for (String string : resourceLists) {
				RoleResourceRel roleResourceRel = new RoleResourceRel();
				roleResourceRel.setId(UUID.randomUUID().toString());
				roleResourceRel.setRoleId(roleId);
				roleResourceRel.setResourceId(string);
				roleResourceRel.setCreateIp("0.0.0.0");
				roleResourceRel.setCreateTime(DateUtils.getCurrDateTimeStr());
				roleResourceRel.setCreateUser("xxx");
				roleResourceRel.setClientLicenseId(clientLicenseId);
				roleResourceRelMapper.insertSelective(roleResourceRel);
			}
		}
		
	}

	@Override
	public List<String> selectRoleResourceIds(String roleId) {
		List<String> returnResourceIdsList = new ArrayList<String>();
		Map<Object, Object> selectRoleIdMap = new HashMap<Object, Object>();
		selectRoleIdMap.put("roleId", roleId);
		List<RoleResourceRel> roleResourceRelList = roleResourceRelMapper.selectListByMap(selectRoleIdMap);
		if (roleResourceRelList != null) {
			for (RoleResourceRel roleResourceRel : roleResourceRelList) {
				returnResourceIdsList.add(roleResourceRel.getResourceId());
			}
		}
		return returnResourceIdsList;
	}

}
